<template>
    <div class="shopcar-container">
        <!-- 商品列表区域 -->
        <div class="goods-list">
            <div class="mui-card" v-for="(item, i) in goodsList" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">

                        <mt-switch v-model="$store.getters.getGoodsSelected[item.id]" @change="changSelected(item.id, $store.getters.getGoodsSelected[item.id])"></mt-switch>

                        <img :src="item.imgUrl">
                        <div class="info">
                            <h1>{{ item.title }}</h1>
                            <p>
                                <span class="price">￥{{ item.sellPrice }}</span>
                                <numbox :initCount="$store.getters.getGoodsCount[item.id]" :goodsId="item.id"></numbox>
                                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <!-- 结算区域  -->
        <div class="mui-card">
            
            <div class="mui-card-content">
                <div class="mui-card-content-inner settlement">
                    <div class="left">
                        <p>总计(不包含运费)</p>
                        <p>已勾选商品 <span class="red">{{ $store.getters.getGoodsCountAndAmount.count }}</span> 件, 
                        总价 <span class="red">￥{{ $store.getters.getGoodsCountAndAmount.amount }}</span></p>
                    </div>

                    <mt-button type="danger">去结算</mt-button>
                </div>
                
            </div>
        </div>

    </div>
</template>

<script>
import numbox from '../goods/ShopCar_Numbox.vue'
//导入mui中Toast组件
import { Toast } from 'mint-ui'
export default {
    data() {
        return {
            goodsList: []
        }
    },
    methods: {
        getShopCarGoodsList() {
            //1.获取store中购物车列表 遍历获取其id 拼接成ids
            var ids = []
            this.$store.state.car.forEach(item => {
                ids.push(item.id)
            });

            if (ids.length <= 0) {
                return ;
            } else {
                //2.使用获取到ids 调用异步请求 获取数据列表
                this.$http.get('vue/getShopCarList/' + ids.join(',')).then(result => {
                    if (result.body.status === 0) {
                        this.goodsList = result.body.message
                    } else {
                        Toast('获取商品信息失败')
                    }
                });
            //3.拼接数据
            }
        },
        remove(id,index) {
            //id是用来删除store中的数据的 index 是用来删除data中goodsList 中的数据的

            this.goodsList.splice(index,1)

            this.$store.commit('removeFormCar',id)


        },
        changSelected(id,status) {
            this.$store.commit('updateShopCarWithGoodsSelected',{id: id,selected: status})
        }
    },
    components: {
        numbox
    },
    created() {
        this.getShopCarGoodsList()
    }
}
</script>

<style lang="scss" scoped>

    .shopcar-container {
        background-color: #eee;
        overflow: hidden;
        .goods-list {

            .mui-card-content-inner {
                //流式布局
                display: flex;
                align-items: center;
            }

            .mui-card {

                img {
                    width: 60px;
                    height: 60px;
                }

                h1 {
                    font-size: 13px;
                }
                .info {
                    display: flex;
                    flex-direction: column;
                    justify-content: space-between;
                    .price {
                        color: red;
                        font-weight: bold;
                    }
                }
            }
        }

        .settlement {
            display: flex;
            justify-content: space-between;
            align-items: center;    //纵向居中

            .red {
                color: red;
                font-weight: bold;
                font-size: 16px;
            }
        }
    }

</style>